git config --global user.name "maxsu"
git config --global user.email "yiibai.com@gmail.com"

# 门禁上路径是/home/pool_002/harmony_master
WORK_DIR=${PWD}
mkdir -p ${WORK_DIR}/llvm

# llvmts
# llvmts + patch : ${WORK_DIR}/llvm-test-suite
git clone https://gitee.com/liwentao_uiw/llvm-test-suite.git ${WORK_DIR}/llvm-test-suite/
(cd ${WORK_DIR}/llvm-test-suite/; git reset --hard 638914)

git clone https://gitee.com/liwentao_uiw/llvmopen-source-transfer-gitee.git -b ohos_toolchain ${WORK_DIR}/llvmopen-source-transfer-gitee/
(cd ${WORK_DIR}/llvmopen-source-transfer-gitee ; cp ./LLVMTS/638914_to_ed298b.patch ${WORK_DIR}/llvm-test-suite/;)
(cd ${WORK_DIR}/llvm-test-suite; cat 638914_to_ed298b.patch | patch -d ./ -p1 -E)
# sh 路径是 : ${WORK_DIR}/llvmts_scripts
(mkdir -p ${WORK_DIR}/llvmts_scripts;)
(cd ${WORK_DIR}/llvmopen-source-transfer-gitee/llvm_test_script ; cp generate_config.py run_x86_test.sh run_x86_O2-test.sh ${WORK_DIR}/llvmts_scripts/ )


# prepare llvm source
cd ${WORK_DIR}/llvm
# 如果是第一次执行 init 会弹出repo Testing colorized output ,需要输入一次Y
(sleep 10; echo y;) | repo init -u https://gitee.com/OpenHarmony/manifest.git -b master -m llvm-toolchain.xml
echo y
repo sync -c
#(sleep 10; echo y;) | repo init -u https://gitee.com/liwentao_uiw/manifest.git -b llvm-ci
#repo sync -c

# Core LLVM toolchain components
# git clone https://gitee.com/openharmony/kernel_liteos_a.git ./kernel/liteos_a/
# git clone https://gitee.com/openharmony/kernel_linux_patches.git
# git clone https://gitee.com/openharmony/prebuilts_lite_sysroot.git ./prebuilts/lite/sysroot/
# third_party dependencies
# git clone https://gitee.com/openharmony/third_party_mingw-w64.git  ./third_party/mingw-w64/ -b mingw-w64_v10.0.0
# git clone https://gitee.com/openharmony/third_party_musl.git ./third_party/musl/
# git clone https://gitee.com/openharmony/third_party_zlib.git ./third_party/zlib/
# git clone https://gitee.com/openharmony/third_party_Linux_Kernel.git ./third_party/Linux_Kernel/
# git clone https://gitee.com/openharmony/third_party_optimized_routines.git ./third_party/optimized-routines/
# git clone https://gitee.com/openharmony/third_party_NuttX.git ./third_party/NuttX/

mkdir -p toolchain
# 此时路径为 /home/pool_002/llvm
# 需要把 work_dir 中的 llvm-project 和 lldb-mi 放入路径 llvm/toochain中
cp -r  ${WORK_DIR}/llvm/third_party/lldb-mi  ./toolchain
cp -r  ${WORK_DIR}/llvm/toolchain/llvm-project/llvm-build ./toolchain
#把门禁拉取的仓库复制进llvm
#cp -r ${WORK_DIR}/kernel ${WORK_DIR}/llvm
#cp -r ${WORK_DIR}/kernel_linux_patches ${WORK_DIR}/llvm
#cp -r ${WORK_DIR}/prebuilts ${WORK_DIR}/llvm
#cp -r ${WORK_DIR}/third_party ${WORK_DIR}/llvm

# update prebuilts
cp ${WORK_DIR}/llvmopen-source-transfer-gitee/llvm_env_script/env_prepare.sh ./toolchain/llvm-project/llvm-build/env_prepare.sh
bash ./toolchain/llvm-project/llvm-build/env_prepare.sh

# build
#python3 ./toolchain/llvm-build/build.py --no-build windows
python3 ${WORK_DIR}/llvm/toolchain/llvm-build/build.py --no-lto --no-build windows
# sysroot的路径在 llvm/out/ohos-sysroot-dev.tar.bz2 , 解压后在 /home/openharmony/llvm_tool/sysroot
mkdir -p ${WORK_DIR}/llvm_tool/
tar -jxvf ${WORK_DIR}/llvm/out/ohos-sysroot-dev.tar.bz2 -C ${WORK_DIR}/llvm_tool/
tar -jxvf ${WORK_DIR}/llvm/out/clang-dev-linux-x86_64.tar.bz2 -C ${WORK_DIR}/llvm_tool/
mv ${WORK_DIR}/llvm_tool/clang-dev ${WORK_DIR}/llvm_tool/clang

# start to test
cd ${WORK_DIR}/llvmopen-source-transfer-gitee/llvm_test_script

# install missing dependency
# pip3 install pyyaml==5.1.2 -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

TIME=$(date +%Y%m%d%H%M%S)
time_dir=$(date +%Y-%m-%d)
llvmts_logdir="${WORK_DIR}/llvmts-log/${time_dir}"

# make log
mkdir -p  ${llvmts_logdir}


#llvmts
pushd ${WORK_DIR}/llvmts_scripts
bash run_x86_test.sh 2>&1 | tee ${llvmts_logdir}/x86_64-linux-hos_${TIME}.txt
bash run_x86_O2-test.sh 2>&1 | tee ${llvmts_logdir}/x86_64_linux-gnu-O2_${TIME}.txt
popd


# 正则会出来结果会是 Failed               :   x，通过tr - 去除字母字符保留数字
X86=1
X86_O2=1
# 如果文件存在进行判断
if [ -e ${llvmts_logdir}/x86_64-linux-hos_${TIME}.txt ]; then
   X86="$(grep -E "[A-Z]+[a-z]+\s+:" ${llvmts_logdir}/x86_64-linux-hos_${TIME}.txt | grep "Failed" | tr -d [A-Z][a-z]' ': )"
fi

if [ -e ${llvmts_logdir}/x86_64_linux-gnu-O2_${TIME}.txt ]; then
    X86_O2="$(grep -E "[A-Z]+[a-z]+\s+:" ${llvmts_logdir}/x86_64_linux-gnu-O2_${TIME}.txt | grep "Failed" | tr -d [A-Z][a-z]' ': )"
fi

# 返回门禁 ：1以上都为失败
# 因为正则扫描的是错误,如果没有错误则为空, 如果 x86 非空并且大于等于1
if [[ -n "$X86" && $X86 -ge 1 ]]; then
    exit 1
fi
if [[  -n "$X86_O2" && $X86_O2 -ge 1 ]]; then
    exit 1
fi